using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class Topic : BaseItem
    {
        private Topic() : base() { }
        private Topic(Hashtable table) : base(table) { }
        private Topic(Hashtable table, List<Topic> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new TopicDal();
        }
        
        public static Topic CreateInstance()
        {
            return new Topic();
        }

        private static Topic Get(Hashtable table)
        {
            if (table == null)
            {
                return null;
            }
            else
            {
                return new Topic(table);
            }

        }

        private static List<Topic> Get(Hashtable[] tables)
        {
            if (tables == null)
            {
                return null;
            }
            else
            {
                List<Topic> tmp = new List<Topic>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new Topic(table, tmp));
                }
                return tmp;
            }
        }

        public static Topic GetById(int id)
        {
            TopicDal dal = new TopicDal();
            return Get(dal.GetById(id));
        }

        public static List<Topic> GetAllBySiteId(int siteId)
        {
            TopicDal dal = new TopicDal();
            return Get(dal.GetAllBySiteId(siteId));
        }

        public static List<Topic> GetAllByPostId(int postId)
        {
            TopicDal dal = new TopicDal();
            return Get(dal.GetAllByPostId(postId));
        }

        protected override void Load(System.Collections.Hashtable table)
        {
            id = (int)table[TopicDal.Fields.Id];
            siteId = (int)table[TopicDal.Fields.SiteId];
            name = (string)table[TopicDal.Fields.Name];
            sequence = (int)table[TopicDal.Fields.Sequence];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }

        private int siteId;
        public int SiteId
        {
            get { return siteId; }
            set { siteId = value; }
        }


        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int sequence = -1;
        public int Sequence
        {
            get { return sequence; }
            set { sequence = value; }
        }

        private Dictionary<string, TopicFile> topicFiles;
        public TopicFile GetAttachment(string attachmentName)
        {
            if (topicFiles == null)
            {
                topicFiles = new Dictionary<string, TopicFile>();
            }
            if(!topicFiles.ContainsKey(attachmentName))
            {
                TopicFile tmp = TopicFile.GetByTopicIdAndName(this.id, attachmentName);
                if (tmp != null)
                {
                    topicFiles.Add(attachmentName, tmp);
                }
            }
            if (topicFiles.ContainsKey(attachmentName))
            {
                return topicFiles[attachmentName];
            }
            else
            {
                return null;
            }
        }

        public new void Update()
        {
            base.Update();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            ((TopicDal)dal).Update(ref id, siteId, name, ref sequence, ref concurencyId);
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((TopicDal)dal).Delete(id, concurencyId);
        }
    }
}
